#include "ADUDuck.h"


USING_NS_CC;



void Duck::setPosition(const Vec2 &position){
	Sprite::setPosition(position);
}

Duck::Duck() : b2BuoyancyMergeListener::b2BuoyancyMergeListener() {
	this->setTag(TAG_DUCK);
	isDied = false;
    m_health = 100;
    waterfalled = false;
    deltaT = 0.0f;
    mLastBindBasin = NULL;
}

Duck::~Duck() {

}

Duck* Duck::create(const string& file) {
	
	Duck* pSprite = new Duck();
	if (pSprite && pSprite->initWithFile(file)) {
		pSprite->autorelease();
		return pSprite;
	}

	CC_SAFE_DELETE(pSprite);

	return NULL;
}

void Duck::initialState() {
	this->setState(DUCK_IDLE);
	this->setHealthStatus(DUCK_HEALTH);
}

void Duck::setState(const int& _state) {
	m_state = _state;
}

int Duck::getState() const {
	return m_state;
}

void Duck::setHealthStatus(const int& _currentHealth) {
	m_health = _currentHealth;
    healthBar->setPercent(m_health);
}
int Duck::getHealthStatus() const {
	return m_health;
}

void Duck::update(float dt) {
	sync();
    deltaT += (dt*GameManager::getInstance()->gameLevel)/2;
    if (deltaT > 1){
        deltaT = 0;
        if (m_health > 0)
        m_health -= 1;
        healthBar->setPercent(m_health);
    }
	if(isDied && waterfalled) {
		m_duckBody->ApplyLinearImpulse(b2Vec2(0,-15.0f), m_duckBody->GetWorldCenter(), true);
	}
}

void Duck::integrateBox2D() {
	Vec2 pos = this->getPosition();
	b2BodyDef bodyDef;
	bodyDef.type = b2_dynamicBody;
	bodyDef.userData = this;

	bodyDef.position.Set(pos.x/PTM_RATIO, pos.y/PTM_RATIO);
	bodyDef.fixedRotation = true;
	m_duckBody = GameManager::getInstance()->mWorld->CreateBody(&bodyDef);
    GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache();
    sc->addFixturesToBody(m_duckBody, FIXTURE_DUCK, DENSITY_DUCK, FRICTION_DUCK, RESTITUTION_DUCK);
    this->setAnchorPoint(sc->anchorPointForShape(FIXTURE_DUCK));
}

void Duck::syncWithPhysics() {
	Vec2 pos = ccp(PTM_RATIO * m_duckBody->GetPosition().x,PTM_RATIO * m_duckBody->GetPosition().y);
	float rotation = -1 * CC_RADIANS_TO_DEGREES(m_duckBody->GetAngle());	
	this->setPosition(pos);
	this->setRotation(rotation);
}

void Duck::syncWithSprite() {
	Vec2 pos = this->getPosition();
	float rotation = this->getRotation();
	m_duckBody->SetTransform(b2Vec2(pos.x/PTM_RATIO, pos.y/PTM_RATIO), rotation);
}

void Duck::sync() {
	syncWithPhysics();
}

void Duck::SetLastBindBasin(Basin* basin){
    mLastBindBasin = basin;
}


void Duck::onStartMerge(void){
    Rect rect = mLastBindBasin->getBoundingBox();
    Vec2 pos = this->getPosition() + Vec2(0,10);
    
    if (rect.containsPoint(pos)) return;
    
    if(!isDied) {
    	isDied = true;
        waterfalled = true;
    	fallInWater();	
    }
}

void Duck::addHealthBar() {
	Size duckSize = this->getContentSize();
    healthBar = Slider::create();
    healthBar->loadBarTexture("images/mana_bar.png");
    healthBar->loadProgressBarTexture("images/mana_bar_red.png");
    healthBar->setPercent(m_health);
    healthBar->setPosition(Vec2(duckSize.width/2, duckSize.height));
    this->addChild(healthBar);
}

void Duck::fallInWater() {
	SpriteFrameCache* cache = SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("Intro/ugly_fall_water.plist", "Intro/ugly_fall_water.png");
	Vector<SpriteFrame*> animFrames(5);
    char str[100] = {0};
    for(int i=6; i < 9; i++) {
        sprintf(str, "Ugly_fall_00%d.png", i);
        SpriteFrame* frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(str);
        animFrames.pushBack(frame);
    }
    Animation* runAnimation = Animation::createWithSpriteFrames(animFrames, 1.0f);    
    this->runAction(RepeatForever::create(Animate::create(runAnimation)));
    m_health = 0;
    healthBar->setPercent(m_health);
}